package ru.urfc.portal.dao.impl;

import java.text.MessageFormat;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ru.urfc.portal.model.Branch;
import ru.urfc.portal.model.SysObject;
import ru.urfc.portal.model.TmpContragent;
import ru.urfc.portal.model.radio.DeviceCertificate;

public class BaseDao extends BaseUtil {

	@PersistenceContext(name="sys", unitName = "sys")
	private EntityManager sysEntityManager;
	
	@PersistenceContext(name="branch1", unitName = "branch1")
	private EntityManager branch1EntityManager;

	@PersistenceContext(name="branch2", unitName = "branch2")
	private EntityManager branch2EntityManager;
	
//	@PersistenceContext(name="branch3", unitName = "branch3")
//	private EntityManager branch3EntityManager;
//
//	@PersistenceContext(name="branch4", unitName = "branch4")
//	private EntityManager branch4EntityManager;
	
	@PersistenceContext(name="eis", unitName = "eis")
	private EntityManager eisEntityManager;
	
	protected EntityManager choseEntityManager(Branch branch, Class<?> klass) {
		if (SysObject.class.isAssignableFrom(klass)) {
			return sysEntityManager;
		}
		if (DeviceCertificate.class.isAssignableFrom(klass)) {
			return eisEntityManager;
		}
		if (TmpContragent.class.isAssignableFrom(klass)) {
			return sysEntityManager;
		}
		if (branch.codeEquals(Branch.SVERD)) {
			return branch1EntityManager; 
		}
		if (branch.codeEquals(Branch.KURGAN)) {
			return branch2EntityManager; 
		}
//		if (branch.codeEquals(Branch.TYUMEN)) {
//			return branch3EntityManager; 
//		}
//		if (branch.codeEquals(Branch.CHEL)) {
//			return branch4EntityManager; 
//		}
		return null;
	}

	public String format(String pattern, Object ... arguments) {
		return MessageFormat.format(pattern, arguments);
	}	
	
	protected EntityManager getSysEntityManager() {
		return sysEntityManager;
	}


}
